const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

const durationForHuman = duration => {
	let minutes = parseInt(duration / 60);
	let seconds = duration % 10;
	// console.log(duration, minutes, seconds);
	if (minutes.length === 0) {
		minutes = '0' + minutes.toString();
	}
	if (seconds.length === 0) {
		seconds = '0' + seconds.toString();
	}
	return minutes + '分钟' + seconds + '秒';
}

const loginCheck = () => {
	if (!uni.getStorageSync('access_token')) {
		uni.showModal({
			title: "提示",
			content: "登录后内容更精彩，是否前往登录?",
			success(res) {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages/auth/login',
					})
				}
			}
		})
	}
}

const go = (page, auth = false) => {
	if (auth && !uni.vk.callFunctionUtil.checkToken()) {

		// #ifdef MP
		uni.showModal({
			title: "提示",
			content: "登录后内容更精彩，是否前往登录?",
			success(res) {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages/login/index/index?redirect=' + encodeURIComponent(page),
					})
				}
			}
		})
		// #endif

		// #ifndef MP
		uni.navigateTo({
			url: '/pages/login/index/index?redirect=' + encodeURIComponent(page),
		})
		// #endif

		return
	}

	uni.navigateTo({
		url: page,
	})
}

const isIos = () => {
	let info = uni.getSystemInfoSync();
	return info.platform === 'ios';
}

const platform = () => {
	let platform = '';
	// #ifdef APP-PLUS
	platform = 'APP';
	// #endif

	// #ifdef H5
	if (isWechat()) {
		platform = 'WECHAT-H5';
	} else {
		platform = 'H5';
	}
	// #endif

	// #ifdef MP
	platform = 'MP-ALIPAY';
	// #endif

	// #ifdef MP-WEIXIN
	platform = 'MP-WEIXIN';
	// #endif

	// #ifdef MP-ALIPAY
	platform = 'MP-ALIPAY';
	// #endif

	return platform;
}

const getWeiXinLoginPlatfo = () => {
	let platform = undefined;

	// #ifdef APP-PLUS
	platform = 'app-plus';
	// #endif

	// #ifdef H5
	if (isWechat()) {
		platform = 'h5-weixin';
	}
	// #endif

	// #ifdef MP-WEIXIN
	platform = 'mp-weixin';
	// #endif

	return platform;
}

const isIosWechatMini = () => {
	let systemInfo = uni.getSystemInfoSync();
	return process.env.VUE_APP_PLATFORM === 'mp-weixin' && systemInfo.platform === 'ios';
}
const isWechatMini = () => {
	return process.env.VUE_APP_PLATFORM === 'mp-weixin';
}
const isH5 = () => {
	return process.env.VUE_APP_PLATFORM === 'h5';
}
const isIosApp = () => {
	let i = false;
	// #ifdef APP-PLUS
	i = uni.getSystemInfoSync().platform === 'ios';
	// #endif
	return i;
}
const isWechat = () => {
	if (typeof window === 'undefined') {
		return false;
	}
	let ua = window.navigator.userAgent.toLowerCase();
	return /micromessenger/.test(ua) ? true : false;
}
const get = (key) => {
	if (typeof window === 'undefined') {
		return null;
	}
	let url = window.location.href.split('?');
	let query = url.length === 1 ? '' : url[1];
	let vars = query.split("&");
	for (let i = 0; i < vars.length; i++) {
		let pair = vars[i].split("=");
		if (pair[0] == key) {
			return pair[1];
		}
	}
	return null;
}
const chunk = (array, subGroupLength) => {
	var index = 0;
	var newArray = [];

	while (index < array.length) {
		newArray.push(array.slice(index, index += subGroupLength));
	}

	return newArray;
}
const random = (minNum, maxNum) => {
	switch (arguments.length) {
		case 1:
			return parseInt(Math.random() * minNum + 1, 10);
			break;
		case 2:
			return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
			break;
		default:
			return 0;
			break;
	}
}

const transOrderType = (type) => {
	var constType = {
		"course": "点播课",
		"articel": "资讯",
		"pay-article": "图文",
		"free-live-course": "公开课",
		"q-bank": "题库",
		"bbs": "问答",
	}

	return constType[type] || "未知的订单类型"
}

const transOrderStatus = (code) => {
	var payStatus = [{
		"label": "待付款",
		"value": 1
	}, {
		"label": "已付款",
		"value": 2
	}, {
		"label": "退款审核中",
		"value": 3
	}, {
		"label": "退款中",
		"value": 4
	}, {
		"label": "已退款",
		"value": 5
	}, {
		"label": "已取消付款/退款",
		"value": -1
	}, {
		"label": "退款拒绝",
		"value": -2
	}, {
		"label": "退款失败",
		"value": -3
	}]
	let status = payStatus.find(v => v.value == code)
	if (!status) {
		status = {
			"label": "未知状态",
			"value": code
		}
	}
	let color = '#606266'
	switch (code) {
		case 2:
			color = '#42b983'
			break
		case 5:
			color = 'red'
			break
	}
	return {
		...status,
		color
	}
}

// // 获取微信的accss_token
// const getAccessToken = () => {
// 	if (!uni.getStorageSync('accessToken')) {
// 		vk.callFunction({
// 			url: 'client/pub/index',
// 			title: '正在获取access_token', // loading的title
// 			success: (res) => {
// 				uni.setStorageSync('accessToken', res.data.token)
// 			}
// 		})
// 	}
// }

// // 发送模板消息
// const sendTemplate = () => {
// 	vk.callFunction({
// 		url: 'client/pub/template',
// 		title: '发送模板消息', // loading的title
// 		data: {
// 			openId: uni.getStorageSync('openid'),
// 			accessToken: uni.getStorageSync('accessToken'),
// 			template_id: 'ZR1hWTAUDqQyUYp5l_HrB8jmVjKySKgpGj9B8Haok_8',
// 			data: {
// 				thing2: {
// 					value: '测试名称'
// 				},
// 				thing7: {
// 					value: '测试姓名'
// 				},
// 				character_string6: {
// 					value: 'fhdjashfkjladhsjkfhakjlhf'
// 				},
// 				amount5: {
// 					value: '0.12'
// 				},
// 				time3: {
// 					value: '2019年12月11日 18:36'
// 				},
// 			}
// 		},
// 		success: (res) => {
// 			console.log(res)
// 		},
// 		error: (err) => {
// 			console.log(err)
// 		}
// 	})
// }

module.exports = {
	formatTime: formatTime,
	durationForHuman: durationForHuman,
	loginCheck: loginCheck,
	go: go,
	isIos: isIos,
	platform: platform,
	getWeiXinLoginPlatfo: getWeiXinLoginPlatfo,
	isIosWechatMini: isIosWechatMini,
	isWechatMini: isWechatMini,
	isH5: isH5,
	isIosApp: isIosApp,
	isWechat: isWechat,
	get: get,
	chunk: chunk,
	random: random,
	transOrderType: transOrderType,
	transOrderStatus: transOrderStatus,
}